<?php
// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class SysUserModel extends Model
{
    
    protected $name = 'sys_user';
    protected $autoWriteTimestamp = true;//时间自动更新 create_time ,update_time


    public $title = '名称';



    
   
    public function edit($param)
    {
        try{
          
            $result =  $this->update($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('sysuser/index'), '修改用户成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


    public function add($param)
    {
        try{
          
            $result =  $this->save($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('sysuser/index'), '添加用户成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function getById($id)
    {
        return $this->where('id', $id)->find();
    }
    
    public function delById($id)
    {
        try{
            $this->where('id', $id)->update(['isdelete'=>'1']);
            return msg(1, '', '删除成功');
        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

  



    /**
     * 根据ID获取信息
     */
    public function getOneById($id)
    {
        return $this->where('id', $id)->find();
    }


    /**
     * 根据ID删除信息
     */
    public function delOneById($id)
    {
        try{

            $this->where('id', $id)->delete();
            return msg(1, '', '删除'.$title.'成功');

        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }



    
    /**
     * 添加一条数据，并检验
     */
    public function addOne($param)
    {
       
        try{

            $vname=ucfirst($name).'Validate';
            $result =  $this->validate($vname)->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url($name.'/index'), '添加'.$title.'成功');
            }
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }


    /**
     * 根据ID修改数据
     */
    public function updateOne($param)
    {
        try{

            $vname=ucfirst($name).'Validate';
            $result =  $this->validate($vname)->save($param, ['id' => $param['id']]);

            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url($name.'/index'), '编辑'.$title.'成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


   /**
     * 根据name查找
     */
    public function findOneByusername($name)
    {
        return $this->where('username', $name)->find();
    }





       /**
     * 更新登录信息
     */
    public function updateLogin($param,$id)
    {
        try{
            $this->where('id', $id)->update($param);
            return msg(1, '', '');
        }catch (\Exception $e){

            return msg(-1, '', $e->getMessage());
        }
    }



    public function getByWhere($where, $offset, $limit)
    {
        return $this->where($where)->limit($offset, $limit)->order('id desc')->select();
    }

    public function getAllCount($where)
    {
        return $this->where($where)->count();
    }

   /**
     * 根据name查找是否存在
     */
    public function isExistByname($name)
    {
       $one= $this->where('username', $name)->find();

       return empty($hasUser)==false;
    }

    /*
     * <layui 改版> 关联角色表
     */
    public function getRoleData(){
        return $this->hasOne('SysRoleModel',"id","roleid");
    }

    /**
     * <layui 改版> 获取管理员数据
     * @param $param
     * @return mixed
     */
    public function getSysUser($param){

        if(!empty($param['page'])){
            $page = $param['page'];
            $limit = $param['limit'];
            if($param['username']){
                $username = $param['username'];
                $where = "isdelete=0 and username like '%".$username."%' ";
            }else{
                $where = "isdelete=0 ";
            }
            $result = self::with('getRoleData')->where($where)->page($page)->limit($limit)->select();
        }else{
            $where = "isdelete=0 ";
            $result = self::with('getRoleData')->where($where)->select();
        }

        $count = self::where($where)->count();
        if($result){
            foreach($result as $key=>$user){
                //dump($user->toArray());exit;
                $user->rolename=$user->getRoleData['rolename'];
                $list[] = $user->data;
            }

            $ret['data'] = $list;
            $ret['code'] = 0;
            $ret['msg']  = '获取数据成功';
            $ret['count'] = $count;
        }else{
            $ret['data'] = '';
            $ret['code'] = -1;
            $ret['msg']  = '获取数据失败';
            $ret['count'] = 0;
        }
        return $ret;
    }

    /**
     * <layui 改版新增的操作方法>
     * @param $param
     * @return mixed
     */
    public function ajaxSaveData($param){

        $type = $param['type'];

        if($type!='del' && $type!='edit2' && $type!='edit'){
            $username = $param['username'];
            $where = "isdelete=0 and username='$username'";
            $result = $this->where($where)->find();//新增或者编辑之前，检查新用户名是否存在

            if($result){
                $data = $result->toArray();
                if($data['username']==$username){
                    $ret['data'] = '';
                    $ret['code'] = -1;
                    $ret['msg']  = '改用户名已经存在，请重新添加';
                    return $ret;
                }

            }
        }

        if($type=='add'){
            if($param['password']){
                $param['password'] = md5($param['password'] . config('salt'));//密码加密
            }
            $param['isdelete'] = 0;
            $param['create_time'] = time();
            $result = self::allowField(true)->save($param);
            if($result){
                $msg = "添加成功";
            }else{
                $msg = "添加失败";
            }
        }
        if($type=='edit' || $type=='edit2'){

            if(!empty($param['newpassword'])){
                $param['password'] = md5($param['newpassword'] . config('salt'));//密码加密
            }else{
                unset($param['password']);//新密码没有填写的时候不更新密码
            }
            $param['update_time'] = time();

            $result = self::allowField(true)->save($param,['id'=>$param['id']]);

            if($result){
                $msg = "编辑成功";
            }else{
                $msg = "编辑失败";
            }
        }
        if($type=='del'){
            $data['id'] = $param['id'];
            $param['isdelete'] = 1;
            $param['update_time'] = time();
            $result = self::allowField(true)->save($param,['id'=>$data['id']]);
            if($result){
                $msg = "删除成功";
            }else{
                $msg = "删除失败";
            }
        }

        if($result){
            $ret['data'] = '';
            $ret['code'] = 1;
            $ret['msg']  = $msg;
        }else{
            $ret['data'] = '';
            $ret['code'] = -1;
            $ret['msg']  = $msg;
        }
        return $ret;
    }

}
